package parser;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class MessageParser
{
	public MessageParser() {}
	
    public ChatMessage getChatMessage(Element docEle)
    {
    	// get the "from" tag
		NodeList users = docEle.getElementsByTagName("from");
		Element userFrom = (Element)users.item(0);
		
		// get the "to" tag
		NodeList users2 = docEle.getElementsByTagName("to");
		Element userTo = (Element)users2.item(0);
		
		// get the "username" tag values from the "from" and "to" tags
		String from = getTextValue(userFrom,"username");
		String to = getTextValue(userTo,"username");
		
		// get the "timestamp" tag value
		String timestamp = getTextValue(docEle, "timestamp");
		
		// get the "text" tag value
		String text = getTextValue(docEle, "text");
		
		// construct a ChatMessage and return it
		ChatMessage chatMessage = new ChatMessage("Chat",from,to,timestamp,text);
		return chatMessage;
    }
    
    public AddBuddyMessage getAddBuddyMessage(Element docEle)
    {
    	// get the "from" tag
		NodeList user = docEle.getElementsByTagName("from");
		Element userFrom = (Element)user.item(0);
		
		// get the "to" tag
		NodeList user2 = docEle.getElementsByTagName("to");
		Element userTo = (Element)user2.item(0);
		
		// get the "username" tag values from the "from" and "to" tags
		String from = getTextValue(userFrom,"username");
		String to = getTextValue(userTo,"username");
				
		// get the "text" tag value
//		String text = getTextValue(docEle, "text");
		
		// construct a AddBuddyMessage and return it
		AddBuddyMessage adMessage = new AddBuddyMessage("AddBuddy",from,to);
		return adMessage;
    }
    
    public RemoveBuddyMessage getRemoveBuddyMessage(Element docEle) {
    	// get the "from" tag
		NodeList user = docEle.getElementsByTagName("from");
		Element userFrom = (Element)user.item(0);
		
		// get the "to" tag
		NodeList user2 = docEle.getElementsByTagName("to");
		Element userTo = (Element)user2.item(0);
		
		// get the "username" tag values from the "from" and "to" tags
		String from = getTextValue(userFrom,"username");
		String to = getTextValue(userTo,"username");
				
		// get the "text" tag value
//		String text = getTextValue(docEle, "text");
		
		// construct a AddBuddyMessage and return it
		RemoveBuddyMessage rmMessage = new RemoveBuddyMessage("RemoveBuddy",from,to);
		return rmMessage;
	}
    
    public ReturnStatusMessage getReturnStatusMessage(Element docEle) {
    	// get the "from" tag
		NodeList userTag = docEle.getElementsByTagName("username");
		Element user = (Element)userTag.item(0);
		
		// get the "to" tag
		NodeList statusTag = docEle.getElementsByTagName("status");
		Element status = (Element)statusTag.item(0);
		
		// get the "username" tag values from the "from" and "to" tags
		String userstr = getTextValue(user,"username");
		String statusstr = getTextValue(status,"status");
				
		// get the "text" tag value
//		String text = getTextValue(docEle, "text");
		
		// construct a AddBuddyMessage and return it
		ReturnStatusMessage rsMessage = new ReturnStatusMessage("ReturnStatus", userstr, Integer.parseInt(statusstr));
		return rsMessage;
	}
    
    /**
	 * I take a xml element and the tag name, look for the tag and get
	 * the text content 
	 * i.e for <employee><name>John</name></employee> xml snippet if
	 * the Element points to employee node and tagName is name I will return John  
	 * @param ele
	 * @param tagName
	 * @return
	 */
	public String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}

	
	/**
	 * Calls getTextValue and returns a int value
	 * @param ele
	 * @param tagName
	 * @return
	 */
	public int getIntValue(Element ele, String tagName) {
		//in production application you would catch the exception
		return Integer.parseInt(getTextValue(ele,tagName));
	}

	

	
}
